package com.li.service.impl;

import com.li.entities.Customer;
import com.li.mapper.CustomerMapper;
import com.li.service.CustomerService;
import com.li.util.CheckUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;
@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {
    public static final String CACHE_KEY_CUSTOMER="customer:";

    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private CheckUtils checkUtils;
    public void addCustomer(Customer customer) {
        int i = customerMapper.insertSelective(customer);
        if(i>0){
            //mysql 插入成功，需要重新查询一次将数据查出来,写进Redis
            Optional<Customer> resultOptional = Optional.ofNullable(customerMapper.selectByPrimaryKey(customer.getId()));
            if (resultOptional.isPresent()) {
            Customer result = customerMapper.selectByPrimaryKey(customer.getId());
            //Redis 缓存key
            String key = CACHE_KEY_CUSTOMER+result.getId();
            //查出来数据写进Redis
            redisTemplate.opsForValue().set(key,result);}
        }
    }
//    public Customer findCustomerById(Integer customerId){
//        Customer customer=null;
//        //缓存key的名称
//        String key = CACHE_KEY_CUSTOMER+customerId;
//        //先去查询redis
//        customer = (Customer) redisTemplate.opsForValue().get(key);
//        //redis有数据直接返回,没有数据查询MySQL
//        if(customer ==null){
//            //再去查询MySQL
//            customer = customerMapper.selectByPrimaryKey(customerId);
//            if(customer!=null){
//                //查出来数据写进Redis
//                redisTemplate.opsForValue().set(key,customer);
//            }
//        }
//        return customer;
//    }

    public Customer findCustomerById(Integer customerId) {
        // 缓存 key 的名称
        String key = CACHE_KEY_CUSTOMER + customerId;
        // 先去查询 Redis
        Optional<Customer> customerOptional = Optional.ofNullable((Customer) redisTemplate.opsForValue().get(key));

        if (customerOptional.isPresent()) {
            return customerOptional.get();
        } else {
            // 再去查询 MySQL
            customerOptional = Optional.ofNullable(customerMapper.selectByPrimaryKey(customerId));
            if (customerOptional.isPresent()) {
                Customer customer = customerOptional.get();
                // 查出来数据写进 Redis
                redisTemplate.opsForValue().set(key, customer);
                return customer;
            }
        }
        return null;
    }

    /**
     * BloomFilter+Redis+MySQL
     * 白名单WhitelistCustomer
     * @param customerId
     * @return
     */
    @Override
    public Customer findCustomerByIdWithBloomFilter(Integer customerId) {
        // 缓存 key 的名称
        String key = CACHE_KEY_CUSTOMER + customerId;
        //todo bloomFilter----------------------------------------------------------
        /**
         * 如果
         */
        if(!checkUtils.checkWithBloomFilter("whitelistCustomer",key)){
            log.info("白名单无此顾客,不可访问:"+key);
            return null;
        }
//-------------------------------------------------------------------------------
        // 先去查询 Redis
        Optional<Customer> customerOptional = Optional.ofNullable((Customer) redisTemplate.opsForValue().get(key));

        if (customerOptional.isPresent()) {
            return customerOptional.get();
        } else {
            // 再去查询 MySQL
            customerOptional = Optional.ofNullable(customerMapper.selectByPrimaryKey(customerId));
            if (customerOptional.isPresent()) {
                Customer customer = customerOptional.get();
                // 查出来数据写进 Redis
                redisTemplate.opsForValue().set(key, customer);
                return customer;
            }
        }
        return null;
    }
}
